/***********************************************\
|	"Warehouse's database, Alexander Yushkov"	|
|	- main.cpp - main source file,				|
|		includes main loop,formatting functions	|
|		some of them are used in the file		|
|		connects.cpp, are declared there too	|
|						15.11.2012				|
\***********************************************/

#include <iostream>
#include <iomanip>
#include "connects/connects.h"

using namespace std;
using namespace lmf;

void border();
void line(const char*);
void ask(const char*);
void hint();

int main()
{
	Base list_base("data/base.dat",Group::size);
	Base log_base("data/log.dat",sizeof(Record));
	List list=extract_base(list_base);
	List_s log=extract_log(log_base);

	vistream vcin('\n');

	border();
	line("Storage's database");
	border();
	hint();
	int desigion;
	int flag=1;
	while(flag)
	{
		ask("Enter an option:");
		try
		{
			vcin.set_del('\n');
			vcin >> desigion;
		}
		catch(...)
		{
			desigion=-1;
		}
		border();
		switch(desigion)
		{
			case 1:
				add_group(list);
				break;
			case 2:
				get_group(list,log);
				break;
			case 3:
				print(list);
				break;
			case 4:
				print(log);
				break;
			case 5:
				stat_full(list,log);
				break;
			case 6:
				stat_range(list,log);
				break;
			case 7:
				dump_base(list_base,list);
				dump_log(log_base,log);
				break;
			case 8:
				hint();
				break;
			case 9:
				line("Allright");
				border();
				flag=0;
				break;
			default:
				line("Unknown option");
				border();
				break;
		}
	}

	dump_base(list_base,list);
	dump_log(log_base,log);
    return 0;
}

void border()
{
	cout << '|' << setfill('-') << right << setw(60) << '|' << endl;
	cout << setfill(' ');
	return;
}

void line(const char* message)
{
	cout << setw(5) << left << '|' << setw(55) << message << '|' << endl;
	return;
}

void ask(const char* message)
{
	cout << setw(5) << left << '|' << message << ' ';
	return;
}

void hint()
{
	line("1 - add a new group");
	line("2 - get some items");
	line("3 - warehouse's state");
	line("4 - hisory of sales");
	line("5 - full statistics");
	line("6 - statistics in range");
	line("7 - save data");
	line("8 - show this hint");
	line("9 - save and exit");
	border();
}
